﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;
using System.Collections;

namespace Yuanlin.Business
{
    public class CommentBreadCrumb : Literal
    {
        private Queue crumbs = new Queue();
        private HtmlAnchor anchor = null;
        private int newsType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.NewsType);
        private int entityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.NewsId);

        protected override void CreateChildControls()
        {
            string href = SiteUrls.Instance().GetNewsListUrl();
            int entityType=0;
            string text = string.Empty;
            if (newsType == (int)CategoryType.CompanyNews)
            {
                href = SiteUrls.Instance().GetCompanyNewsListUrl();
                crumbs.Enqueue(GetAnchor("新闻列表首页", href));

                if (entityId <= 0)
                {
                    return;
                }
                CompanyNews companyNews = BusinessManager.GetCompanyNews(entityId);
                if (companyNews == null)
                {
                    return;
                }
                entityId = companyNews.EntityId.Value;
                entityType = (int)CategoryType.CompanyNews;
                text = companyNews.Title.Value.Length > 40 ? companyNews.Title.Value.Substring(0, 40) + "..." : companyNews.Title.Value;

            }
            else
            {
                crumbs.Enqueue(GetAnchor("新闻列表首页", href));

                if (entityId <= 0)
                {
                    return;
                }
                News c1 = BusinessManager.GetNews(entityId);
                if (c1 == null)
                {
                    return;
                }
                entityId = c1.EntityId.Value;
                entityType = (int)CategoryType.News;
                text = c1.Title.Value.Length > 40 ? c1.Title.Value.Substring(0, 40) + "..." : c1.Title.Value;
            }

            crumbs.Enqueue(GetAnchor(text, SiteUrls.Instance().GetCommentListUrl(entityId, entityType)));
        }

        protected Control GetAnchor(string innerText, string href)
        {
            anchor = new HtmlAnchor();
            anchor.InnerHtml = innerText;
            anchor.Attributes["class"] = "crumbAnchor";
            anchor.HRef = href;

            return anchor;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("新闻导航:&nbsp;&nbsp;");

            while (crumbs.Count > 0)
            {
                HtmlAnchor a = (HtmlAnchor)crumbs.Dequeue();

                a.Title = a.InnerHtml;

                if ((crumbs.Count > 0) && (a.InnerHtml.Length > 30))
                {
                    a.InnerHtml = a.InnerHtml.Substring(0, 30) + "...";
                }
                a.RenderControl(writer);

                if (crumbs.Count > 0)
                {
                    writer.Write(" &#187; ");
                }
            }

        }

    }
}
